﻿
using System;
using System.ComponentModel;
using System.Linq.Expressions;
namespace MyFramework.Common
{
    /// <summary>
    /// extension method
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="projection">The projection.</param>
        /// <returns></returns>
        public static string GetPropertyName<TProperty>(this Expression<Func<TProperty>> projection)
        {
            var memberExpression = (MemberExpression)projection.Body;
            return memberExpression.Member.Name;
        }

        /// <summary>
        /// Raises the changing.
        /// </summary>
        /// <param name="propertyChanging">The property changing.</param>
        /// <param name="o">The automatic.</param>
        /// <param name="propertyName">Name of the property.</param>
        public static void RaiseChanging(this PropertyChangingEventHandler propertyChanging, IEntity o, string propertyName)
        {
            if (propertyChanging != null)
            {
                var e = new PropertyChangingEventArgs(propertyName);
                propertyChanging(o, e);
            }
        }

        /// <summary>
        /// Raises the changed.
        /// </summary>
        /// <param name="propertyChanged">The property changed.</param>
        /// <param name="o">The automatic.</param>
        /// <param name="propertyName">Name of the property.</param>
        public static void RaiseChanged(this PropertyChangedEventHandler propertyChanged, IEntity o, string propertyName)
        {
            if (propertyChanged != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                propertyChanged(o, e);
            }
        }
    }
}
